const Order = require('../models/Order');
const Product = require('../models/Product');
const dayjs = require('dayjs');
const mockData = async () => {
    // 产品名称列表
    const productNames = [
        '智能手机', '笔记本电脑', '平板电脑', '智能手表', '蓝牙耳机',
        '无线充电器', '移动电源', '数据线', '手机壳', '屏幕保护膜'
    ];

    // 产品分类
    const categories = ['电子产品', '手机配件', '电脑硬件', '智能设备', '音频设备'];

    // 生成随机产品数据
    const generateProducts = async (count = 20) => {
        const products = [];

        for (let i = 0; i < count; i++) {
            let t = dayjs(new Date(Date.now() - Math.floor(Math.random() * 15) * 24 * 60 * 60 * 1000)).valueOf()
            const product = new Product({
                name: productNames[Math.floor(Math.random() * productNames.length)] + ' ' + (i + 1),
                price: parseFloat((Math.random() * 1000 + 10).toFixed(2)),
                category: categories[Math.floor(Math.random() * categories.length)],
                createdAt: t
            });

            products.push(product);
        }

        // 保存到数据库
        await Product.insertMany(products);
        console.log(`成功生成 ${count} 条产品数据`);
        return products;
    };

    // 生成随机订单数据
    const generateOrders = async (products, count = 30) => {
        const orders = [];

        for (let i = 0; i < count; i++) {
            const product = products[Math.floor(Math.random() * products.length)];
            const quantity = Math.floor(Math.random() * 5) + 1;
            let t = dayjs(new Date(Date.now() - Math.floor(Math.random() * 15) * 24 * 60 * 60 * 1000)).valueOf()
            const order = new Order({
                productId: product._id,
                quantity: quantity,
                total: parseFloat((product.price * quantity).toFixed(2)),
                orderTime: t
            });

            orders.push(order);
        }

        // 保存到数据库
        await Order.insertMany(orders);
        console.log(`成功生成 ${count} 条订单数据`);
    };
    const products = await generateProducts();
    await generateOrders(products);


}
module.exports = {
    mockData
}
